package com.bit.lotterySystem.controller.handler;

import com.bit.lotterySystem.common.errorcode.GlobalErrorCodeConstants;
import com.bit.lotterySystem.common.exception.ControllerException;
import com.bit.lotterySystem.common.exception.ServiceException;
import com.bit.lotterySystem.common.pojo.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author wangw
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    private final static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    @ExceptionHandler
    public CommonResult<?> exceptionHandler(Exception e) {
        logger.error("服务错误，",e);
        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),
                e.getMessage());
    }

    @ExceptionHandler(value = ServiceException.class)
    public CommonResult<?> serviceExceptionHandler(ServiceException e) {
        logger.error("serviceException,", e);
        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),
                e.getMessage());
    }

    @ExceptionHandler(value = ControllerException.class)
    public CommonResult<?> controllerExceptionHandler(ControllerException e) {
        logger.error("controllerException,", e);
        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR);
    }

}
